<template>
    <el-container>
        <el-main class="cate_mana_main">
            <div style="text-align:center;">
                <div>
                    迭代次数:
                    <el-input placeholder="如：2" v-model="modelForm.NumberOfIterations"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    种群规模:
                    <el-input placeholder="如：60" v-model="modelForm.populationSize"
                              style="width: 200px;margin-top: 10px;"></el-input>
                </div>
                <div>
                    基因个数:
                    <el-input placeholder="如：2" v-model="modelForm.numberOfGenes"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    头部长度:
                    <el-input placeholder="如：15" v-model="modelForm.headLength"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    基因突变率:
                    <el-input placeholder="如：0.044" v-model="modelForm.geneMutationRate"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    IS转座率:
                    <el-input placeholder="如：0.1" v-model="modelForm.IsTranspositionRate"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    RIS转座率:
                    <el-input placeholder="如：0.1" v-model="modelForm.RISTranspositionRate"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    基因转座率:
                    <el-input placeholder="如：0.1" v-model="modelForm.geneTranspositionRate"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    单点重组率:
                    <el-input placeholder="如：0.3" v-model="modelForm.singlePointRecombinationRate"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    两点重组率:
                    <el-input placeholder="如：0.3" v-model="modelForm.twoPointRestructuringRate"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    基因重组率:
                    <el-input placeholder="如：0.1" v-model="modelForm.geneRecombinationRate"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    选择策略:
                    <el-select placeholder="如：锦标赛" v-model="modelForm.selectionStrategy"
                               style="width: 200px;margin-top: 10px;">
                        <el-option
                                v-for="item in selection"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                    <br/>
                </div>
                <div>
                    适应度函数:
                    <el-select placeholder="如：相对误差" v-model="modelForm.fitnessFunction"
                               style="width: 200px;margin-top: 10px;">
                        <el-option
                                v-for="item in fitness"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                    <br/>
                </div>
                <div>
                    连接基因的头部长度:
                    <el-input placeholder="如：15" v-model="modelForm.theHeadLengthOfTheLinkedGene"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    连接基因的变异率:
                    <el-input placeholder="如：0.5" v-model="modelForm.theVariationRateOfLinkageGenes"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    模型名字
                    <el-input placeholder="如：氯离子" v-model="modelForm.modelName"
                              @input="verifyModelName" style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
                <div>
                    拟合模型存放路径:
                    <el-input placeholder="如：D:\model" v-model="modelForm.fittingModelStoragePath"
                              style="width: 200px;margin-top: 10px;"></el-input>
                    <br/>
                </div>
            </div>
            <el-upload class="upload_btn"
                       action="uploadUrl"
                       :http-request="onUpload"
                       :show-file-list="false"
                       >
                <div slot="tip" class="el-upload__tip">只能上传.txt文件</div>
                <el-button slot="trigger" size="small" type="primary" >选择训练数据文件</el-button>
            </el-upload>
            <el-button type="primary" style="width: 150px;text-align: center;margin:10px auto;"
                       @click="train">训练
            </el-button>
        </el-main>
    </el-container>
</template>
<script>
    import {deleteRequest, getRequest,  postRequestBody, putRequest} from '../utils/api'
    import {postRequestModelName, postUpload} from "../tool/pushFile";
    export default {
        mounted: function () {
            let user = sessionStorage.getItem("user");
            if (user!= null) {
        this.currentUser= JSON.parse(user);
        this.identity=this.currentUser[0].identity;
        console.log(this.identity);
                if (this.identity=="普通用户"){
                    this.$alert('普通用户无法建模！');
                }
    }
        },
        methods: {
            onUpload(multipartFile) {// 上传文件
                let formData = new FormData()
                formData.append('file', multipartFile.file)
                postUpload(formData).then((res) => {
                    this.$message.success("上传成功")
                }).catch((e) => {
                    this.$message.error(e.message)
                })
            },
            train() {
                this.verifyModelName()
                this.$axios({
                    method: 'post',
                    url: '/system/model/train',
                    data:[this.modelForm.NumberOfIterations,
                        this.modelForm.populationSize,
                        this.modelForm.numberOfGenes,
                        this.modelForm.headLength,
                        this.modelForm.geneMutationRate,
                        this.modelForm.IsTranspositionRate,
                        this.modelForm.RISTranspositionRate,
                        this.modelForm.geneTranspositionRate,
                        this.modelForm.singlePointRecombinationRate,
                        this.modelForm.twoPointRestructuringRate,
                        this.modelForm.geneRecombinationRate,
                        this.modelForm.selectionStrategy,
                        this.modelForm.fitnessFunction,
                        this.modelForm.theHeadLengthOfTheLinkedGene,
                        this.modelForm.theVariationRateOfLinkageGenes,
                        this.modelForm.modelName,
                        this.modelForm.fittingModelStoragePath],
                timeout:200 * 60 * 1000,
                }).then(resp => {
                    this.$alert('建模成功!', '成功!'),
                        console.log(this.modelForm.modelName);
                    console.log(resp.data);
                    this.$router.push({
                        name: '建模结果',
                        params: {
                            fitness: resp.data.data[0],
                            functionExpression: resp.data.data[1],
                            rootMeanSquareError: resp.data.data[2],
                            fittingCurvePath: resp.data.data[3],
                            modelName: this.modelForm.modelName,
                        },
                    });
                }).catch(err => {
                    this.$alert('无法建模！');
                });
            },
            verifyModelName() {//输入模型名时，先验证其唯一性
                var _this = this;
                postRequestModelName("/system/model/getModelName", this.modelForm.modelName).then(resp => {
                    if (resp.status == 200) {
                        JSON.stringify(resp);
                        if (resp.data.msg == "模型名重复") {
                            _this.$alert('模型名重复，请重新填写!', '失败!');
                        }
                    }
                })
            },
            },
            data() {
                return {
                    modelName: '',
                    selItems: [],
                    categories: [],
                    loading: false,
                    inputData: '',//输入的内容
                    uploadUrl: '/system/model/file',
                    identity:'',
                    modelForm: {
                        NumberOfIterations: null,
                        populationSize: null,
                        numberOfGenes: null,
                        headLength: null,
                        geneMutationRate: null,
                        IsTranspositionRate: null,
                        RISTranspositionRate: null,
                        geneTranspositionRate: null,
                        singlePointRecombinationRate: null,
                        twoPointRestructuringRate: null,
                        geneRecombinationRate: null,
                        selectionStrategy: null,
                        fitnessFunction: null,
                        theHeadLengthOfTheLinkedGene: null,
                        theVariationRateOfLinkageGenes: null,
                        modelName: null,
                        fittingModelStoragePath: null
                    },
                    selection: [{
                        value: '锦标赛',
                        label: '锦标赛'
                    }, {
                        value: '赌盘',
                        label: '赌盘'
                    },
                    ],
                    fitness: [{
                        value: '相对误差',
                        label: '相对误差'
                    }, {
                        value: '绝对误差',
                        label: '绝对误差'
                    },
                    ],
                }
            }
    }
</script>
<style>
    .cate_mana_header {
        background-color: #ececec;
        margin-top: 20px;
        padding-left: 5px;
        display: flex;
        justify-content: flex-start;
    }
    .cate_mana_main {
        /*justify-content: flex-start;*/
        display: flex;
        flex-direction: column;
        padding-left: 5px;
        background-color: #ececec;
        margin-top: 20px;
        padding-top: 10px;
    }
</style>
